<template>
	<view>
		<view class="cell_page">
			<u-cell-group>
				<u-cell size="large" :title="address.name + ' ' + address.phone" isLink url="/pages/cart/address"
					:label="address.province + address.city + address.county + address.address"></u-cell>
			</u-cell-group>
		</view>
		<view class="u-p-b-80">
			<view class="goodsInfo u-flex u-p-10 u-border-bottom" v-for="(item,index) in goodsList" :key='index'>
				<u--image width="200rpx" height="200rpx"
					:src="item.goods.cover_url" shape="circle">
				</u--image>
				<view class="goodsBaseInfo u-flex u-row-between u-col-top u-p-10 u-p-r-20">
					<view class="priceNum">
						<text class="title u-p-l-10 u-font-32">{{item.goods.title}}</text>
					</view>
					<view class="priceNum u-flex u-row-between">
						<text class="price u-font-34">￥{{item.goods.price}}</text>
						<text>X{{item.num}}</text>
					</view>
				</view>
			</view>
		</view>
		<view class="bottomFixed u-flex u-p-15">
			<view class="u-flex u-flex-1 u-row-around">
				<view class="u-flex-5 u-text-center">
					<text class="title">合计：</text>
					<text class="price">￥{{totalPrice}}</text>
				</view>
				<view class="u-flex-5 u-text-center">
					<u-button type="primary" shape="square" :disabled="!hasAddress">结算订单</u-button>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				hasAddress: false, // 是否有地址
				address: {
					name:'',
					phone:''
				}, //收货地址
				goodsList: [], //商品列表
				totalPrice: 0, //商品总价
			}
		},
		async onLoad(options) {
			console.log(options);
			if (!uni.$u.utils.isLogin()) {
				return;
			}
			let access_token = uni.getStorageSync('access_token');
			let res = await uni.$u.http.get('/api/orders/preview', {
				header: {
					Authorization: 'bearer ' + access_token
				}
			})
			console.log(res);
			if(options.id){
				//说明是从地址管理页面跳转过来 ，则应该使用此id对应的地址
				res.address.some(item => {
					if(item.id == options.id){
						//说明当前item中的地址就是在管理地址页面所选择的新的地址
						this.address = item;
						return true; 
					}
				})
			}
			else{
				//说明是从购物车页面过来 
				if (res.address.length) {
					//说明有地址
					this.hasAddress = true;
					//查找是否有默认地址
					if (!res.address.some((item) => {
							if (item.is_default) {
								this.address = item;
								return true;
							}
						})) {
						this.address = res.address[0];
					}
				}
			}			
			this.goodsList = res.carts;
			this.totalPrice = this.goodsList.reduce((pre,goods)=>{
				return parseInt(pre) + parseInt(goods.goods.price * goods.num);
			},0)
		},
		methods: {

		}
	}
</script>

<style scoped lang="scss">
	.cell_page {
		padding-bottom: 20px;
	}

	/deep/ .u-cell__title-text--large {
		font-size: 18px;
		font-weight: bold;
	}

	.goodsInfo {
		width: 100%;
		background-color: rgb(251, 250, 251);

		.goodsBaseInfo {
			width: 100%;
			height: 200rpx;
			flex-direction: column;

			.priceNum {
				width: 100%;
			}
		}
	}

	.price {
		font-weight: 800;
		color: #e83333;
	}

	.bottomFixed {
		position: fixed;
		left: 0;
		right: 0;
		bottom: 0;
		background-color: #fff;
	}


	/*#ifdef MP-WEIXIN*/
	.bottomFixed {
		z-index: 99999;
	}

	/*#endif*/
</style>
